home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Kit PC World De Ampliacion De Windows 95
/
Kit PC World de ampliacion de Windows 95.iso
/
internet
/
sweeper
/
samples
/
asyncstg
/
barrfact.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-03
|
4KB
|
189 lines
#include "barrfact.h"
#include "barrobj.h"
#include "tchar.h"
ULONG g_dwRefCount=0;
// Create a new database object and return a pointer to it
HRESULT CAsyncByteArrayFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void** ppObject)
{
if (pUnkOuter && riid!=IID_IUnknown)
{
*ppObject=NULL;
return E_INVALIDARG;
}
CAsyncByteArray* pObj=new CAsyncByteArray();
HRESULT hRes=pObj->Initialize(pUnkOuter);
if (FAILED(hRes))
{
delete pObj;
return hRes;
}
if (pUnkOuter)
{
*ppObject=(IUnknown*) (pObj->m_punkInner);
pObj->m_punkInner->AddRef();
}
else if (FAILED(pObj->QueryInterface(riid, ppObject)))
{
delete pObj;
*ppObject=NULL;
return E_NOINTERFACE;
}
return S_OK;
}
HRESULT CAsyncByteArrayFactory::LockServer(BOOL fLock)
{
if (fLock)
{
InterlockedIncrement((long*) &g_dwRefCount);
}
else
{
InterlockedDecrement((long*) &g_dwRefCount);
}
return S_OK;
}
CAsyncByteArrayFactory::CAsyncByteArrayFactory()
{
m_dwRefCount=0;
}
HRESULT CAsyncByteArrayFactory::QueryInterface(REFIID riid, void** ppObject)
{
if (riid==IID_IUnknown || riid==IID_IClassFactory)
{
*ppObject=(IClassFactory*) this;
}
else
{
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
ULONG CAsyncByteArrayFactory::AddRef()
{
InterlockedIncrement((long*) &g_dwRefCount);
InterlockedIncrement((long*) &m_dwRefCount);
return m_dwRefCount;
}
ULONG CAsyncByteArrayFactory::Release()
{
ULONG dwRefCount=m_dwRefCount-1;
InterlockedDecrement((long*) &g_dwRefCount);
if (InterlockedDecrement((long*) &m_dwRefCount)==0)
{
delete this;
return 0;
}
return dwRefCount;
}
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void** ppObject)
{
if (rclsid==CLSID_AsyncByteArrayWrapper)
{
CAsyncByteArrayFactory *pFactory= new CAsyncByteArrayFactory;
if (FAILED(pFactory->QueryInterface(riid, ppObject)))
{
delete pFactory;
*ppObject=NULL;
return E_INVALIDARG;
}
}
else
{ // here you could check for additional CLSID's you DLL may provide
return CLASS_E_CLASSNOTAVAILABLE;
}
return NO_ERROR;
}
HRESULT _stdcall DllCanUnloadNow()
{
if (g_dwRefCount)
{
return S_FALSE;
}
else
{
return S_OK;
}
}
STDAPI DllRegisterServer(void)
{
HKEY hKeyCLSID, hKeyInproc32;
DWORD dwDisposition;
if (RegCreateKeyEx(HKEY_CLASSES_ROOT,
_T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}"),
NULL, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKeyCLSID, &dwDisposition)!=ERROR_SUCCESS)
{
return E_UNEXPECTED;
}
if (RegSetValueEx(hKeyCLSID, "", NULL, REG_SZ, (BYTE*) "Asynchronous Byte Array Wrapper", sizeof("Asynchronous Byte Array Wrapper"))!=ERROR_SUCCESS)
{
RegCloseKey(hKeyCLSID);
return E_UNEXPECTED;
}
HMODULE hModule;
if (RegCreateKeyEx(hKeyCLSID,
"InprocServer32",
NULL, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKeyInproc32, &dwDisposition)!=ERROR_SUCCESS)
{
RegCloseKey(hKeyCLSID);
return E_UNEXPECTED;
}
hModule=GetModuleHandle(_T("ASYNCSTG.DLL"));
if (!hModule)
{
RegCloseKey(hKeyInproc32);
RegCloseKey(hKeyCLSID);
return E_UNEXPECTED;
}
TCHAR szName[MAX_PATH+1];
if (GetModuleFileName(hModule, szName, sizeof(szName))==0)
{
RegCloseKey(hKeyInproc32);
RegCloseKey(hKeyCLSID);
return E_UNEXPECTED;
}
if (RegSetValueEx(hKeyInproc32, "", NULL, REG_SZ, (BYTE*) szName, sizeof(TCHAR)*(lstrlen(szName)+1))!=ERROR_SUCCESS)
{
RegCloseKey(hKeyInproc32);
RegCloseKey(hKeyCLSID);
return E_UNEXPECTED;
}
RegCloseKey(hKeyInproc32);
RegCloseKey(hKeyCLSID);
return NOERROR;
}
STDAPI DllUnregisterServer(void)
{
if (RegDeleteKey(HKEY_CLASSES_ROOT,
_T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}\\InprocServer32"))!=ERROR_SUCCESS)
{
return E_UNEXPECTED;
}
if (RegDeleteKey(HKEY_CLASSES_ROOT,
_T("CLSID\\{30DF3437-0266-11cf-BAA6-00AA003E0EED}"))!=ERROR_SUCCESS)
{
return E_UNEXPECTED;
}
return NOERROR;
}